package com.only4play.authcenter.infrastructure.security.providers.admin.sms;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

/**
 * @author hinzzz www.hinzzz.cn 157957329@qq.com
 * 2024-03-05
 */
@Slf4j
public class SmsProviders implements AuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        SmsToken token = (SmsToken) authentication;
        log.info("短信登录逻辑开始");
        log.info("手机号：{} 验证码{}", token.getPhone(), token.getVerifyCode());
        String phone = token.getPrincipal().toString();
        String verityCode = token.getCredentials().toString();
        /*User user = userService.getUserByPhone(phone);
        if(null == user){
            throw new BadCredentialsException(CodeMsg.passwordIncorrect.getMsg());
        }
        //查询db 进行校验
        SmsRecord smsRecord = smsRecordService.getSmsRecordByPhone(phone);
        if(null == smsRecord){
            throw new BadCredentialsException(CodeMsg.passwordIncorrect.getMsg());
        }
        if(smsRecord.getCode().equals(verityCode)){
            List<GrantedAuthority> grantedAuthorities = userService.getUserRolesByUserId(user.getId());
            JwtUser sessionInfo = new JwtUser(user.getUsername(),"",grantedAuthorities,true,"");
            return new LoginSuccessToken(grantedAuthorities,sessionInfo);
        }else {
            throw new AuthenticationServiceException(CodeMsg.passwordIncorrect.getMsg());
        }*/
        return authentication;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.isAssignableFrom(SmsToken.class);
    }
}
